home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / udphdr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  1.3 KB  |  55 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "ip.h"
  4. #include "internet.h"
  5. #include "udp.h"
  6.  
  7. /* Convert UDP header in internal format to an mbuf in external format */
  8. struct mbuf *
  9. htonudp(udp,data,ph)
  10. struct udp *udp;
  11. struct mbuf *data;
  12. struct pseudo_header *ph;
  13. {
  14.     struct mbuf *bp;
  15.     register char *cp;
  16.     int16 checksum;
  17.  
  18.     /* Allocate UDP protocol header and fill it in */
  19.     if((bp = pushdown(data,UDPHDR)) == NULLBUF)
  20.         return NULLBUF;
  21.  
  22.     cp = bp->data;
  23.     cp = put16(cp,udp->source);    /* Source port */
  24.     cp = put16(cp,udp->dest);    /* Destination port */
  25.     cp = put16(cp,udp->length);    /* Length */
  26.     *cp++ = 0;            /* Clear checksum */
  27.     *cp-- = 0;
  28.  
  29.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  30.      * the spec requires us to change zeros into ones to distinguish an
  31.       * all-zero checksum from no checksum at all
  32.      */
  33.     if((checksum = cksum(ph,bp,ph->length)) == 0)
  34.         checksum = 0xffffffff;
  35.     put16(cp,checksum);
  36.     return bp;
  37. }
  38. /* Convert UDP header in mbuf to internal structure */
  39. int
  40. ntohudp(udp,bpp)
  41. struct udp *udp;
  42. struct mbuf **bpp;
  43. {
  44.     char udpbuf[UDPHDR];
  45.  
  46.     if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR)
  47.         return -1;
  48.     udp->source = get16(&udpbuf[0]);
  49.     udp->dest = get16(&udpbuf[2]);
  50.     udp->length = get16(&udpbuf[4]);
  51.     udp->checksum = get16(&udpbuf[6]);
  52.     return 0;
  53. }
  54.  
  55.